package com.xindz.hgq.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import com.lztx.shared.shared.util.glide.GlideApp;
import com.orhanobut.logger.Logger;
import com.shizhefei.view.largeimage.LargeImageView;
import com.shizhefei.view.largeimage.factory.FileBitmapDecoderFactory;
import com.xindz.hgq.R;
import com.xindz.hgq.base.BaseActivity;
import com.xindz.hgq.base.options.BaseActivityOptions;


import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name lz
 * @class name：com.liandaeast.jiankangyuan.module.record.view
 * @describe 高清大图加载
 * @anthor XINDZ
 * @time 2018/4/27 12:49
 * @change
 * @chang time
 * @class describe
 */
public class LongImageActivity extends BaseActivity {


    @BindView(R.id.bt_image_load_1)
    Button btImageLoad1;
    @BindView(R.id.bt_image_load_2)
    Button btImageLoad2;

    @BindView(R.id.bt_image_load_0)
    Button btImageLoad0;
    @BindView(R.id.iv_image)
    LargeImageView ivImage;

    private String url = "http://lc-0smsjobq.cn-n1.lcfile.com/d119f23e82389c756ba4.png";

    @Override
    public BaseActivityOptions setOption() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_long_image);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_image_load_1, R.id.bt_image_load_2, R.id.bt_image_load_0})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_image_load_0:
                ivImage.setImage(R.drawable.mine_bg_head);
                break;
            case R.id.bt_image_load_1:
                GlideApp.with(this)
                        .asBitmap()
                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                                int height = resource.getHeight();
                                Logger.i("Bitmap.getHeight:" + height);
                                ivImage.setImage(resource);
                                return false;
                            }
                        }).load(url)
                        .submit();
                break;
            case R.id.bt_image_load_2:
                GlideApp.with(this)
                        .asFile()
                        .listener(new RequestListener<File>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                                Bitmap bitmap = ImageUtils.bytes2Bitmap(FileIOUtils.readFile2BytesByChannel(resource));
                                if (bitmap != null) {
                                    int height = bitmap.getHeight();
                                    Logger.i("Bitmap.getHeight:" + height);
                                }
                                String absolutePath = resource.getAbsolutePath();
                                String path = resource.getPath();
                                long length = resource.length();
                                Logger.i("File.getAbsolutePath:" + absolutePath + "****File.getPath:" + path + "****File.length:" + length);
                                ivImage.setImage(new FileBitmapDecoderFactory(resource));
                                return false;
                            }
                        }).load(url)
                        .submit();
                break;
        }
    }


}
